/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Public License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/opensource/licenses.mspx#Ms-PL

 ****************************************************************************************/
using System;

namespace Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary
{
    public class PickListItem
    {
        #region private variables

        private System.Xml.XmlElement m_XmlElement;

        private string m_Label;
        private string m_Value;

        private Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickList m_PickList;

        #endregion

        internal PickListItem(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickList i_PickList, string i_Value) :
            this (i_PickList, i_Value, i_Value)
        {
        }

        internal PickListItem(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickList i_PickList, string i_Label, string i_Value)
        {
            this.PickListItemInitialize
                (
                i_PickList,
                i_Label,
                i_Value
                );

            string ElementName = Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.PICKLIST_ITEM_ELEMENT_NAME;
            if (!System.String.IsNullOrEmpty(m_PickList.Binding.BindingCollection.NamespacePrefix))
                ElementName = m_PickList.Binding.BindingCollection.NamespacePrefix + ":" + ElementName;

            m_XmlElement = m_PickList.Binding.BindingCollection.RSSFeed.XmlDocument.CreateElement
                (
                ElementName,
                Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.SIMPLE_FEED_BINDINGS_XML_NAMESPACE_URI
                );

            m_XmlElement.SetAttribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.LABEL_ATTRIBUTE, m_Label);
            m_XmlElement.InnerText = m_Value;
        }

        internal PickListItem(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickList i_PickList, System.Xml.XmlElement i_PickListItemXmlElement)
        {
            bool InvalidElement =
                (i_PickListItemXmlElement.LocalName != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.PICKLIST_ITEM_ELEMENT_NAME) ||
                (i_PickListItemXmlElement.NamespaceURI != Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.SIMPLE_FEED_BINDINGS_XML_NAMESPACE_URI);

            if (InvalidElement)
                throw new System.ArgumentException("Invalid i_PickListItemXmlElement!");

            if (i_PickListItemXmlElement.OwnerDocument != i_PickList.XmlElement.OwnerDocument)
                i_PickListItemXmlElement = (System.Xml.XmlElement)i_PickList.XmlElement.OwnerDocument.ImportNode(i_PickListItemXmlElement, true);

            m_XmlElement = i_PickListItemXmlElement;

            string Label = m_XmlElement.GetAttribute(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.Constants.LABEL_ATTRIBUTE);
            string Value = m_XmlElement.InnerText;

            this.PickListItemInitialize
                (
                i_PickList,
                Label,
                Value
                );
        }

        internal void PickListItemInitialize(Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickList i_PickList, string i_Label, string i_Value)
        {
            if (System.String.IsNullOrEmpty(i_Value))
                throw new System.ArgumentException("Invalid value for i_Value!");

            m_PickList = i_PickList;
            m_Label = i_Value;
            m_Value = i_Value;

            if (!System.String.IsNullOrEmpty(i_Label))
                m_Label = i_Label;
        }

        public Microsoft.CSA.FeedSync.SimpleFeedBindingsLibrary.PickList PickList
        {
            get
            {
                return m_PickList;
            }
        }

        public string Label
        {
            get
            {
                return m_Label;
            }
        }

        public string Value
        {
            get
            {
                return m_Value;
            }
        }

        public System.Xml.XmlElement XmlElement
        {
            get
            {
                return m_XmlElement;
            }
        }

    }
}
